﻿using GalaSoft.MvvmLight.Messaging;
using JiveMessenger;
using JiveMessenger.Log;
using JiveMessenger.Models;
using JiveMessenger.User;
using JiveMessenger.Utilities;
using JiveMessenger.ViewModels;
using JmShared;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.UI;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;


namespace JiveMessenger.Converters
{

    public class ContactListItemWidthConverter : DependencyObject,IValueConverter
    {


        public int NumberOfColumns
        {
            get { return (int)GetValue(NumberOfColumnsProperty); }
            set { SetValue(NumberOfColumnsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NumberOfColumns.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NumberOfColumnsProperty =
            DependencyProperty.Register("NumberOfColumns", typeof(int), typeof(ContactListItemWidthConverter), new PropertyMetadata(1));


        public object Convert(object value, Type targetType, object parameter, string language)
        {
            double controlWidth = (double)value;
            return Math.Abs((controlWidth / NumberOfColumns)-10);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class BoolToInverseBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return !(bool)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class TabToAvatarSourceConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            ViewModelTab tab = value as ViewModelTab;

            if (tab is ViewModelPrivateMessage)
            {
                ViewModelPrivateMessage vm = (ViewModelPrivateMessage)value;

                if (!string.IsNullOrEmpty(vm.PmTarget.Avatar))
                    return ((ViewModelPrivateMessage)value).PmTarget.Avatar;
                else
                    return "ms-appx:///Assets/Images/Person.png";
            }
            else if (tab is ViewModelGroupChat)
            {
                return "ms-appx:///Assets/Images/Group.png";
            }
            else
            {
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }


    public class UserToHamburgerBrushConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            IContact currentUser = null;
            Messenger.Default.Send(new NotificationMessageAction<IContact>("GetCurrentUserContact", contact =>
                {
                    currentUser = contact;
                }));

            if (((IContact)value).UserName == currentUser.UserName)
                return (SolidColorBrush)Application.Current.Resources["DefaultAccentBrush"];
            else
                return (SolidColorBrush)Application.Current.Resources["ContactHamburgerBrush"];

        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    // Converter for disabling ComboboxItem
    public class ComboboxDisableConverter : DependencyObject, IValueConverter
    {

        public JiveChatGroupModel CurrentChatGroup
        {
            get { return (JiveChatGroupModel)GetValue(CurrentChatGroupProperty); }
            set { SetValue(CurrentChatGroupProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CurrentChatGroup.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CurrentChatGroupProperty =
            DependencyProperty.Register("CurrentChatGroup", typeof(JiveChatGroupModel), typeof(ComboboxDisableConverter), new PropertyMetadata(null));




        public IContact CurrentUser
        {
            get { return (IContact)GetValue(CurrentUserProperty); }
            set { SetValue(CurrentUserProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CurrentUser.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CurrentUserProperty =
            DependencyProperty.Register("CurrentUser", typeof(IContact), typeof(ComboboxDisableConverter), new PropertyMetadata(null));


        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null)
                return value;
            GroupPrivilege privilege = GroupPrivilege.Member;
            if (Enum.TryParse(value.ToString(), out privilege))
            {

                GroupPrivilege currentUserPrivilege = CurrentChatGroup.GroupMembers.FirstOrDefault(x => x.Key == CurrentUser.UserName).Value;

                switch (privilege)
                {
                    case GroupPrivilege.Owner:
                        {
                            switch (currentUserPrivilege)
                            {
                                case GroupPrivilege.Owner:
                                    return true;
                                case GroupPrivilege.Moderator:
                                    return false;
                                default:
                                    return false;
                            }
                        }
                    case GroupPrivilege.Moderator:
                        {
                            switch (currentUserPrivilege)
                            {
                                case GroupPrivilege.Owner:
                                    return true;
                                case GroupPrivilege.Moderator:
                                    return false;
                                default:
                                    return false;
                            }
                        }
                    default:
                    case GroupPrivilege.Member:
                        {
                            switch (currentUserPrivilege)
                            {
                                case GroupPrivilege.Owner:
                                    return true;
                                case GroupPrivilege.Moderator:
                                    return true;
                                default:
                                    return false;
                            }
                        }
                }
            }
            return true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class BooleanToQueryButtonIconConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null || !(value is bool))
                return new SymbolIcon(Symbol.Cancel);

            if ((bool)value == true)
                return new SymbolIcon(Symbol.Accept);
            else
                return new SymbolIcon(Symbol.Forward);

        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class FontStyleToToggleButtonCheckedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string val = value != null ? (string)value : "";
            string param = parameter != null ? (string)parameter : "";

            if (val.Equals(param, StringComparison.OrdinalIgnoreCase))
                return true;
            else
                return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            bool isChecked = (bool)value;
            if (isChecked)
                return parameter;
            else
                return "Normal";
        }
    }


    public class ColorToBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return new SolidColorBrush((Color)value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class MessageTypeToVisibilityConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            MessageModelType visibleType = (MessageModelType)Enum.Parse(typeof(MessageModelType), (string)parameter);

            MessageModelType type = (MessageModelType)value;
            if (type == visibleType)
                return Visibility.Visible;

            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }


    public class MessageTextToExpanderVisibilityConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value.ToString().Length > StaticSettings.MaxMessageLength)
                return Visibility.Visible;

            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class PmStatusConverter : DependencyObject, IValueConverter
    {
        public ViewModelMainPage ViewModelMainPage
        {
            get { return (ViewModelMainPage)GetValue(ViewModelMainPageProperty); }
            set { SetValue(ViewModelMainPageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ViewModelMainPage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ViewModelMainPageProperty =
            DependencyProperty.Register("ViewModelMainPage", typeof(ViewModelMainPage), typeof(PmStatusConverter), new PropertyMetadata(null));

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            IContact contact = value as IContact;
            if (ViewModelMainPage.ChatViewModels.Any(x => x.ID == contact.UserName))
                return "Open";
            else
                return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class ConnectionStatusToAppBarColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            bool isLoggedIn = (bool)value;

            if (isLoggedIn)
                return new SolidColorBrush(Colors.Green);
            else
                return new SolidColorBrush(Colors.Maroon);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class PivotIndexToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            int index = (int)value;
            switch (parameter.ToString())
            {

                case "chatpage":
                    return index == 0 ? Visibility.Visible : Visibility.Collapsed;
                case "contactspage":
                    return index == 1 ? Visibility.Visible : Visibility.Collapsed;
                default:
                    return Visibility.Visible;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }


    public class FontColorToUserColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            Color color = (Color)value;

            if (color == Colors.Black && (StaticSettings.CurrentTheme == PhoneTheme.Dark || StaticSettings.CurrentTheme == PhoneTheme.HighContrastDark))
                return new SolidColorBrush(Colors.White);
            else if (color == Colors.White && (StaticSettings.CurrentTheme == PhoneTheme.Light || StaticSettings.CurrentTheme == PhoneTheme.HighContrastLight))
                return new SolidColorBrush(Colors.Black);
            else
                return new SolidColorBrush(color);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }


    public class EmoticonControlHeightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            double result = (double)value;
            if (ApplicationView.GetForCurrentView().Orientation == ApplicationViewOrientation.Portrait)
            {
                result -= 27;//hack: 27 is the margin that the pivot item overflows upwards in portrait mode. Have to figure out that bullshit.
            }

            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class EmoticonControlHeightToPopupOffsetConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            double result = (double)value;
            
            if (ApplicationView.GetForCurrentView().Orientation == ApplicationViewOrientation.Portrait)
            {
                result -= 27;//hack: 27 is the margin that the pivot item overflows upwards in portrait mode. Have to figure out that bullshit.
            }

            return 1 - result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class EmoticonSizeConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            double size = (double)value;
            double margin = double.Parse(parameter.ToString());

            double newVal = ((size - (margin * 2)) / 6) - ((margin * 2));
            return newVal;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }


    /// <summary>
    /// Value converter that translates true to <see cref="Visibility.Visible"/> and false to
    /// <see cref="Visibility.Collapsed"/>.
    /// </summary>
    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value is Visibility && (Visibility)value == Visibility.Visible;
        }

    }



    public class ValidEmoticonToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string error;
            if (VmHelper.Locator.CurrentClient.ValidateEmoticonShortcut((string)value, out error))
                return true;
            else
                return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }

    }
    public class DateTimeToShortTimeStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return ((DateTime)value).ToString("t", CultureInfo.CurrentCulture);//((DateTime)value).ToShortTimeString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class BoolToForegroundDisabledConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            bool enabled = (bool)value;
            if (enabled)
                return new SolidColorBrush(Colors.Black);
            else
                return StaticSettings.DisabledBrush;//App.Current.Resources["JiveDisabledBrush"] as SolidColorBrush;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class ValidEmoticonToSolidColorBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string error;
            if (VmHelper.Locator.CurrentClient.ValidateEmoticonShortcut((string)value, out error))
                return new SolidColorBrush(Colors.Green);
            else
                return new SolidColorBrush(Colors.Black);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class ChatViewModelConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value is ViewModelGroupChat)
                return Application.Current.Resources["DataTemplateGroupChatControl"] as DataTemplate;
            else if (value is ViewModelPrivateMessage)
                return Application.Current.Resources["DataTemplatePrivateMessageControl"] as DataTemplate;
            else if (value is ViewModelLoginControl)
                return Application.Current.Resources["DataTemplateLoginControl"] as DataTemplate;
            else
                return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class NotNullToVisibilityConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return value != null ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class IsNullToVisibilityConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return value == null ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class TabToCanCloseTabConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value is ViewModelTab && ((ViewModelTab)value).IsPinned)
                return false;
            else
                return true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }


    public class ResourceStringToUriConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null)
                return null;
            else if (String.IsNullOrEmpty(value.ToString()))
                return null;

            return new Uri(value.ToString(), UriKind.RelativeOrAbsolute);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return ((Uri)value).AbsoluteUri;
        }


    }
    public class IsNullConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return value == null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class StringNullOrEmptyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value is string)
                return String.IsNullOrEmpty((string)value);
            else
                return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }


    /// <summary>
    /// Returns Visible when number is above 0, returns Collapsed if null or 0
    /// </summary>
    public class NumberToVisibilityConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null || (int)value == 0)
                return Visibility.Collapsed;
            else
                return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class InverseNumberToVisibilityConverter : IValueConverter
    {
        NumberToVisibilityConverter _converter = new NumberToVisibilityConverter();

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var result = _converter.Convert(value, targetType, parameter, language) as Visibility?;
            return result == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }


    /// <summary>
    /// Returns the bitmapimage for an IEmoticonModel
    /// </summary>
    public class CustomEmoticonImagePathConverter : IValueConverter
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            try
            {
                IEmoticon model = value as IEmoticon;
                if (model == null)
                    return null;
                //if (model.Type == EmoticonType.CustomEmoticon)
                //{
                //    return StaticMethods.LoadBitmapImageFromIsolatedStorage(new Uri(model.EmoticonPath, UriKind.RelativeOrAbsolute));
                //}
                //else
                //{
                bool isPathRooted = Path.IsPathRooted(model.EmoticonPath);

                Uri u = new Uri(model.EmoticonPath);// new Uri(string.Format("ms-appx:///{0}", model.EmoticonPath));

                //  var bitmap = StaticMethods.LoadBitmapImageFromIsolatedStorage(u);
                var bitmap = new BitmapImage(u);
                return bitmap;
                //}
            }
            catch (Exception e)
            {
                Logger.ErrorException("Error converting custom emoticon image path", e);
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class InverseBooleanToVisibilityConverter : IValueConverter
    {
        private BooleanToVisibilityConverter _converter = new BooleanToVisibilityConverter();

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var result = _converter.Convert(value, targetType, parameter, language) as Visibility?;
            return result == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            var result = _converter.ConvertBack(value, targetType, parameter, language) as bool?;
            return result == true ? false : true;
        }
    }
    /// <summary>
    /// Returns black brush if true, else red brush.
    /// </summary>
    public class BoolToValidEmoticonBrush : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            bool isValid = (bool)value;
            return isValid ? new SolidColorBrush(Colors.Black) : new SolidColorBrush(Colors.Red);
        }
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Returns the description text for an enum.
    /// </summary>
    public class NotificationTypeToTextConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            NotificationType t = (NotificationType)value;
            return StaticMethods.GetEnumDescription(t);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// Returns the formatted notification message for an INotification.
    /// </summary>
    public class NotificationToTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            NotificationBase notification = (NotificationBase)value;
            string returnText = string.Empty;
            switch (notification.NotificationType)
            {
                case NotificationType.FriendInvite:
                    {
                        var n = (FriendInviteNotification)notification;
                        returnText = string.Format("{0} has sent a friend request {1}", n.SenderUsername, String.IsNullOrEmpty(n.NotificationText) ? string.Empty : ": " + n.NotificationText);
                        break;
                    }
                case NotificationType.GroupInvite:
                    {
                        var n = (GroupInviteNotification)notification;
                        returnText = string.Format("{0} invites you to the group {1} {2}", n.SenderUsername, n.Group.Name, String.IsNullOrEmpty(n.NotificationText) ? string.Empty : ": " + n.NotificationText);
                        break;
                    }
                case NotificationType.ServerMessage:
                    returnText = string.Format("Info: {0}", notification.NotificationText);
                    break;
                case NotificationType.UpdateAvailable:
                    {
                        var n = (UpdateAvailableNotification)notification;

                        returnText = n.NotificationText;
                        break;
                    }
            }
            return returnText;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// Returns a one line string summary for a List of notifications.
    /// </summary>
    public class NotificationListToTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var notifications = value as ICollection<NotificationBase>;
            if (notifications != null)
            {
                string returnVal = string.Empty;
                //  List<List<INotification>> valuesByType = notifications.GroupBy(x => x.NotificationType).Select(x => x.Select(v => v).ToList()).ToList();
                var valuesByType = notifications.ToLookup(x => x.NotificationType);
                foreach (var subGroup in valuesByType)
                {
                    returnVal += string.Format("{0} new {1}, ", subGroup.Count(), StaticMethods.GetEnumDescription(subGroup.Key));
                }
                returnVal = returnVal.TrimEnd().TrimEnd(',');
                return returnVal;
            }
            else
                return string.Empty;
        }
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value;
        }
    }
    /// <summary>
    /// Returns comma separated list of shortcuts for a string array with emoticon shortcuts or a shortcut string
    /// </summary>
    public class EmoticonShortCutToTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value is string)
                return (string)value;
            else if (value is string[])
            {
                string[] shortCutArray = value as string[];
                return shortCutArray.Aggregate((A, B) => A + "," + B);
            }
            else
                return string.Empty;
        }
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value;
        }
    }
    /// <summary>
    /// Returns Visibility.Visible if client is offline, else Visibility.Collapsed.
    /// </summary>
    public class ClientStatusToStatusTextVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            ClientStatus status = (ClientStatus)value;
            if (status == ClientStatus.Offline)
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class ClientStatusToBorderBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            ClientStatus status = (ClientStatus)value;
            switch (status)
            {
                case ClientStatus.Afk:
                    return (LinearGradientBrush)App.Current.Resources["ButtonGradientBrushAFK"];
                case ClientStatus.Available:
                    return (LinearGradientBrush)App.Current.Resources["ButtonGradientBrushOnline"];
                case ClientStatus.Busy:
                    return (LinearGradientBrush)App.Current.Resources["ButtonGradientBrushBusy"];
                case ClientStatus.Offline:
                default:
                    return (LinearGradientBrush)App.Current.Resources["ButtonGradientBrushOffline"];
            }

        }
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class ClientStatusToStatusTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            ClientStatus status = (ClientStatus)value;
            switch (status)
            {
                case ClientStatus.Afk:
                    return "Away";
                case ClientStatus.Available:
                    return "Available";
                case ClientStatus.Busy:
                    return "Busy";
                case ClientStatus.Offline:
                default:
                    return "Offline";
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }


    public class ContactGroupStatusConverter : DependencyObject, IValueConverter
    {


        public JiveClientStatusGroupModel OnlineGroup
        {
            get { return (JiveClientStatusGroupModel)GetValue(OnlineGroupProperty); }
            set { SetValue(OnlineGroupProperty, value); }
        }

        // Using a DependencyProperty as the backing store for OnlineGroup.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OnlineGroupProperty =
            DependencyProperty.Register("OnlineGroup", typeof(JiveClientStatusGroupModel), typeof(ContactGroupStatusConverter), new PropertyMetadata(null));



        public JiveClientStatusGroupModel OfflineGroup
        {
            get { return (JiveClientStatusGroupModel)GetValue(OfflineGroupProperty); }
            set { SetValue(OfflineGroupProperty, value); }
        }

        // Using a DependencyProperty as the backing store for OfflineGroup.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OfflineGroupProperty =
            DependencyProperty.Register("OfflineGroup", typeof(JiveClientStatusGroupModel), typeof(ContactGroupStatusConverter), new PropertyMetadata(null));



        public object Convert(object value, Type targetType, object parameter, string language)
        {
            ClientStatus status = (ClientStatus)value;
            return Convert(status, OnlineGroup, OfflineGroup);
        }
        private static JiveClientStatusGroupModel Convert(ClientStatus status, JiveClientStatusGroupModel onlineGroup, JiveClientStatusGroupModel offlineGroup)
        {
            if (status == ClientStatus.Available || status == ClientStatus.Afk || status == ClientStatus.Busy)
                return onlineGroup;
            else
                return offlineGroup;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class ChatGroupToGroupMemberCountConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {

            if (value is IChatGroup)
            {
                IChatGroup model = (IChatGroup)value;
                switch (model.Type)
                {
                    case GroupType.JiveChatGroup:
                        JiveChatGroupModel group = (JiveChatGroupModel)value;
                        int count = group.GroupMembers.Count;
                        return count > 0 ? "/" + count : string.Empty;
                    default:
                        return string.Empty;
                }

            }
            else
                return string.Empty;
        }
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }



    public class ChatGroupToGroupNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {

            if (value is IChatGroup)
                return ((IChatGroup)value).Name;
            else
                return value;

        }
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class ChatGroupToIgnoreVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value is IChatGroup)
            {
                IChatGroup model = (IChatGroup)value;
                switch (model.Type)
                {
                    case GroupType.JiveChatGroup:
                        return ((JiveChatGroupModel)value).IsBlocked ? Visibility.Visible : Visibility.Collapsed;
                    default:
                        return Visibility.Collapsed;
                }
            }
            else
                return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    public class BooleanToToBlockGroupHeaderText : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null || !(value is bool))
                return "Block Group";

                    return ((bool)value)==true ? "Unblock Group" : "Block Group";
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class BooleanToIgnoreContactHeaderText : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            bool isIgnored = (bool)value;
            return isIgnored ? "Unignore contact" : "Ignore contact";
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }








}
